//
//  ViewController.m
//  Video
//
//  Created by os on 2021/3/26.
//

#import "ViewController.h"

#import "ZLSystemCapture.h"
#import "ZLVideoEncoder.h"
#import "ZLVideoDecoder.h"

#import "AAPLEAGLLayer.h"

@interface ViewController () <ZLSystemCaptureDelegate, ZLVideoEncoderDelegate, ZLVideoDecoderDelegate>

@property (nonatomic, strong) ZLSystemCapture *capture;

@property (nonatomic, strong) ZLVideoEncoder *videoEncoder;
@property (nonatomic, strong) ZLVideoDecoder *videoDecoder;

@property (nonatomic, strong) AAPLEAGLLayer *displayLayer;

@property (nonatomic, strong) NSFileHandle *handle;
@property (nonatomic, copy) NSString *path;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initVideo];
    
}

- (void)initVideo {
    
    _path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"video.h264"];
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:_path]) {
        if ([manager removeItemAtPath:_path error:nil]) {
            NSLog(@"删除成功");
            if ([manager createFileAtPath:_path contents:nil attributes:nil]) {
                NSLog(@"创建文件");
            }
        }
    } else {
        if ([manager createFileAtPath:_path contents:nil attributes:nil]) {
            NSLog(@"创建文件");
        }
    }
    
    NSLog(@"%@", _path);
    _handle = [NSFileHandle fileHandleForWritingAtPath:_path];
    
    [ZLSystemCapture checkCameraAuthor];
    
    // 捕获媒体
    _capture = [[ZLSystemCapture alloc] initWithType:ZLSystemCaptureTypeVideo];
    [_capture prepareWithPreviewsize:CGSizeMake(self.view.frame.size.width/2, self.view.frame.size.height/2)];
    _capture.preview.frame = CGRectMake(0, 50, self.view.frame.size.width/2, self.view.frame.size.height/2);
    [self.view addSubview:_capture.preview];
    _capture.delegate = self;
    
    ZLVideoConfig *config = [ZLVideoConfig defaultConfig];
    config.width = _capture.width;
    config.height = _capture.height;
    config.bitrate = config.width * config.height * 5;
    
    _videoEncoder = [[ZLVideoEncoder alloc] initWithConfig:config];
    _videoEncoder.delegate = self;
    
    _videoDecoder = [[ZLVideoDecoder alloc] initWithConfig:config];
    _videoDecoder.delegate = self;
    
    _displayLayer = [[AAPLEAGLLayer alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, 50, self.view.frame.size.width/2, self.view.frame.size.height/2)];
    [self.view.layer addSublayer:_displayLayer];
    
    [self.capture start];
}

#pragma mark - ZLSystemCaptureDelegate

- (void)captureSampleBuffer:(CMSampleBufferRef)sampleBuffer type:(ZLSystemCaptureType)type {
    if (type == ZLSystemCaptureTypeVideo) {
        [_videoEncoder encodeVideoSampleBuffer:sampleBuffer];
    }
}

#pragma mark - ZLVideoEncoderDelegate

// h264 编码回调( sps / pps)
- (void)videoEncodeCallbackSps:(NSData *)sps pps:(NSData *)pps {
    [_videoDecoder decodeNaluData:sps];
    [_videoDecoder decodeNaluData:pps];
    
    [_handle writeData:sps];
    [_handle writeData:pps];
}

// h264 编码回调( 数据 )
- (void)videoEncodeCallback:(NSData *)h264Data {
    [_videoDecoder decodeNaluData:h264Data];
    
    [_handle writeData:h264Data];
}

#pragma mark - ZLVideoDecoderDelegate

// h264 解码回调
- (void)videoDecodeCallback:(CVPixelBufferRef)imageBuffer {
    if (imageBuffer) {
        _displayLayer.pixelBuffer = imageBuffer;
    }
}

@end
